package cn.gp.chatclient.utils;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

public class ImageUtil {
    
    /**
     * 从资源文件加载并缩放图标
     * @param resourcePath 资源路径，例如 "icons/contacts.png"
     * @param width 目标宽度
     * @param height 目标高度
     * @return 缩放后的ImageIcon
     */
    public static ImageIcon getScaledImageIcon(String resourcePath, int width, int height) {
        try {
            // 从类路径加载资源
            InputStream inputStream = ImageUtil.class.getClassLoader().getResourceAsStream(resourcePath);
            if (inputStream == null) {
                // 如果找不到资源，创建默认图标
                return createDefaultIcon(width, height, "图标");
            }
            
            // 加载原始图片
            ImageIcon originalIcon = new ImageIcon(inputStream.readAllBytes());
            inputStream.close();
            
            // 缩放图片
            Image scaledImage = originalIcon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
            return new ImageIcon(scaledImage);
            
        } catch (IOException e) {
            // 如果加载失败，创建默认图标
            return createDefaultIcon(width, height, "图标");
        }
    }
    
    /**
     * 创建默认图标
     * @param width 宽度
     * @param height 高度
     * @param text 显示的文字
     * @return 默认图标
     */
    private static ImageIcon createDefaultIcon(int width, int height, String text) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();
        
        // 设置抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        // 绘制圆形背景
        g2d.setColor(new Color(70, 130, 180));
        g2d.fillOval(0, 0, width, height);
        
        // 绘制文字
        g2d.setColor(Color.WHITE);
        g2d.setFont(new Font("微软雅黑", Font.PLAIN, Math.max(8, width / 8)));
        FontMetrics fm = g2d.getFontMetrics();
        int x = (width - fm.stringWidth(text)) / 2;
        int y = ((height - fm.getHeight()) / 2) + fm.getAscent();
        g2d.drawString(text, x, y);
        
        g2d.dispose();
        return new ImageIcon(image);
    }
    
    /**
     * 从URL加载头像图标
     * @param avatarUrl 头像URL
     * @param width 目标宽度
     * @param height 目标高度
     * @return 缩放后的ImageIcon
     */
    public static ImageIcon getAvatarIcon(String avatarUrl, int width, int height) {
        if (avatarUrl == null || avatarUrl.trim().isEmpty()) {
            return createDefaultIcon(width, height, "头像");
        }
        
        try {
            // 从URL加载图片
            ImageIcon originalIcon = new ImageIcon(new java.net.URL(avatarUrl));
            Image scaledImage = originalIcon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
            return new ImageIcon(scaledImage);
        } catch (Exception e) {
            // 如果加载失败，创建默认头像
            return createDefaultIcon(width, height, "头像");
        }
    }
}